/*
 * @Author: your name
 * @Date: 2021-02-15 19:52:55
 * @LastEditTime: 2021-04-27 23:32:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jianfa-api\service\projectCodeService.js
 */
const {
    addProjectCodeDao,
    getProjectListDao,
    editProjectDao,
    getPidDao,
    delProCodeDao,
    selectImgByPidDao,
    delImgByPidDao,
} = require("../dao/projectCodeDao");
const { delImg } = require("../utils/utils");
const moment = require("moment");
module.exports = {
    /**
     * @description:
     * @param {Object} obj //保存的数据
     * @return {*}
     */
    async addProjectCodeService(obj) {
        moment.locale("zh-cn");
        const create_time = moment().format("YYYY年MM月D日 h:mm:ss");
        obj.create_time = create_time;
        return await addProjectCodeDao(obj);
    },
    //根据id删除工程
    async delProService({ id }) {
        const { status, results } = await selectImgByPidDao(id); //查询图片路径
        // const { status } = await delImgDao(img_id)//删除图片
        if (status) {
            results.forEach(async (item) => {
                await delImg(item["img_path"]); //删除本地图片
            });
            await delImgByPidDao(id); //删除图片
        }
        return await delProCodeDao(id);
    },
    // 获取项目pid
    async getPidService() {
        return await getPidDao();
    },
    // 获取项目列表
    async getProjectListService(params) {
        const res = await getProjectListDao(params);
        res.results.sort((a, b) => b.sort - a.sort);
        return res;
    },
    // 编辑项目
    async editProjectService(obj) {
        return await editProjectDao(obj);
    },
};
